/**   
 * @Title: TrackingRecordDetailMoreAct.java 
 * @Package: com.esun.rabbit2.viewer.mainact.mylotteryact.trackingrecord 
 * @Description: TODO(显示全部子任务) 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-8-11 下午05:24:51  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.trackingrecord;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.rabbit2.communicaters.trackingrecord.CancleTrackingCommunicater;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryRecordDetailAct;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: TrackingRecordDetailMoreAct
 * @Description: TODO(显示全部的子任务)
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-8-11 下午05:24:51
 */
public class TrackingRecordDetailMoreAct extends Activity implements OnClickListener {

	private LinearLayout linearChildList;
	private Bundle bundle;
	private String lotID;
	private String serverMsg;
	private String mainID;
	private String mainState;
	private int clickedNum;
	private boolean isAllCancle = false;
	private boolean isShow = true;
	private Handler detailHandler;
	private ProgressDialog progressDialog;
	private TextView tvMainState;
	private Button btnCancleAllTracking;
	private ImageButton btnBack;
	private CancleTrackingCommunicater cancleCommunicater;

	private static final int CANCLE_TRACKING_SUCCEED = 1000003;
	private static final int CANCLE_TRACKING_FAILED = 1000004;
	private static final String MAIN_TASK = "1";
	private static final String CHILD_TASK = "0";

	private ArrayList<String> exp = new ArrayList<String>();
	private ArrayList<String> code = new ArrayList<String>();
	private ArrayList<String> id = new ArrayList<String>();
	private ArrayList<String> state = new ArrayList<String>();
	private ArrayList<String> getMoney = new ArrayList<String>();
	private ArrayList<String> cpid = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trackingrecord_detailmore_act);

		initUI();
		initHandler();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (UserInfo.getUserInfo().isRebuy()) {
			this.finish();
		}
	}

	private void initUI() {

		bundle = TrackingRecordDetailMoreAct.this.getIntent().getExtras();
		lotID = bundle.getString("lotid");
		exp = bundle.getStringArrayList("exp");
		code = bundle.getStringArrayList("code");
		id = bundle.getStringArrayList("id");
		state = bundle.getStringArrayList("state");
		getMoney = bundle.getStringArrayList("getMoney");
		cpid = bundle.getStringArrayList("cpid");
		mainID = bundle.getString("mainID");
		mainState = bundle.getString("mainstate");
		EDebug.o(this, "mainstate = " + mainState);

		progressDialog = ViewerUtil.showProgressDialog(this, "请稍后...");
		;

		linearChildList = (LinearLayout) findViewById(R.id.trackingrecord_detailmore_act_linear_list);

		tvMainState = (TextView) findViewById(R.id.trackingrecord_detailmore_act_tv_mainState);
		btnCancleAllTracking = (Button) findViewById(R.id.trackingrecord_detailmore_act_btn_cancleAllTracking);
		btnCancleAllTracking.setOnClickListener(this);

		btnBack = (ImageButton) findViewById(R.id.trackingrecord_detailmore_act_btn_back);
		btnBack.setOnClickListener(this);

		if (mainState.equals("0")) {
			tvMainState.setText("进行中");
		} else if (mainState.equals("1")) {
			tvMainState.setText("已完成");
			btnCancleAllTracking.setEnabled(false);
			btnCancleAllTracking.setVisibility(View.INVISIBLE);
		} else if (mainState.equals("2")) {
			tvMainState.setText("被终止");
			btnCancleAllTracking.setEnabled(false);
			btnCancleAllTracking.setVisibility(View.INVISIBLE);
		}

		childListShow(exp, code, id, state, getMoney, cpid);
	}

	public void initHandler() {
		detailHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);

				switch (msg.what) {

				case CANCLE_TRACKING_SUCCEED:

					if (isAllCancle) {
						for (int i = 0; i < linearChildList.getChildCount(); i++) {
							((Button) linearChildList.getChildAt(i).findViewById(R.id.childtracking_each_line_btn_cancle)).setVisibility(View.GONE);
							((TextView) linearChildList.getChildAt(i).findViewById(R.id.childtracking_each_line_tv_state)).setText("已取消");
						}
						showTipDialog("任务已终止");
						btnCancleAllTracking.setEnabled(false);
						btnCancleAllTracking.setVisibility(View.INVISIBLE);
						tvMainState.setText("被终止");
					} else {
						((Button) linearChildList.getChildAt(clickedNum).findViewById(R.id.childtracking_each_line_btn_cancle)).setVisibility(View.GONE);
						((TextView) linearChildList.getChildAt(clickedNum).findViewById(R.id.childtracking_each_line_tv_state)).setText("已取消");
						if (isAllCancle()) {
							btnCancleAllTracking.setEnabled(false);
							btnCancleAllTracking.setVisibility(View.INVISIBLE);
							tvMainState.setText("被终止");
						}
						showTipDialog("撤单成功");
					}

					break;

				case CANCLE_TRACKING_FAILED:

					showTipDialog(serverMsg);

					break;

				case Config.CONNECTION_FAIL:

					showTipDialog(Language.STR_CONNECTIONFAIL);

					break;

				default:
					break;
				}
				if (progressDialog != null) {
					progressDialog.cancel();
				}
			}

		};
	}

	private void childListShow(final ArrayList<String> exp, ArrayList<String> code, final ArrayList<String> id, ArrayList<String> state, ArrayList<String> getMoney, final ArrayList<String> cpid) {
		for (int i = 0; i < exp.size(); i++) {
			final String winMoney;
			final int _i = i;
			View viewChild = TrackingRecordDetailMoreAct.this.getLayoutInflater().inflate(R.layout.trackingrecord_child_each_line, null);
			viewChild.setTag(i);
			TextView tvExpect = (TextView) viewChild.findViewById(R.id.childtracking_each_line_tv_expect);
			tvExpect.setText(exp.get(i));
			TextView tvChildRed = (TextView) viewChild.findViewById(R.id.childtracking_each_line_tv_numRed);
			TextView tvChildBlue = (TextView) viewChild.findViewById(R.id.childtracking_each_line_tv_numBlue);
			if (!code.get(i).equals("")) {
				String[] strCode = code.get(i).split("\\|");
				tvChildRed.setText(strCode[0].replace(',', ' '));
				tvChildBlue.setText(strCode[1].replace(',', ' '));
			} else {
				tvChildRed.setText(Config.NULLMARK);
				tvChildBlue.setText("未开奖");
				tvChildBlue.setTextColor(getResources().getColor(R.color.solid_black));
			}
			TextView tvState = (TextView) viewChild.findViewById(R.id.childtracking_each_line_tv_state);
			tvState.setTag("state");
			Button btnChild = (Button) viewChild.findViewById(R.id.childtracking_each_line_btn_cancle);
			EDebug.o(this, id.get(i));
			btnChild.setTag("btn");
			btnChild.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					final Dialog tipDialog = new Dialog(TrackingRecordDetailMoreAct.this, R.style.dialog);
					tipDialog.getWindow().setContentView(R.layout.dialog_quit);
					((TextView) tipDialog.getWindow().findViewById(R.id.dialogquit_tv_title)).setText("取消追号任务");
					((TextView) tipDialog.getWindow().findViewById(R.id.dialogquit_tv_msg)).setText("您要取消第" + exp.get(_i) + "期任务吗？");
					((Button) tipDialog.getWindow().findViewById(R.id.dialogquit_btn_cancel)).setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							tipDialog.cancel();
						}
					});
					((Button) tipDialog.getWindow().findViewById(R.id.dialogquit_btn_submit)).setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							isAllCancle = false;
							tipDialog.cancel();
							progressDialog.show();
							clickedNum = _i;
							cancleTrackingThread(CHILD_TASK, id.get(_i));
							EDebug.o(this, id.get(_i));
						}
					});
					tipDialog.show();
				}
			});
			ImageView arrow = (ImageView) viewChild.findViewById(R.id.trackingrecord_child_each_line_iv_arrow);

			if (state.get(i).equals("0")) {
				winMoney = "";
				btnChild.setVisibility(View.VISIBLE);
				arrow.setVisibility(View.INVISIBLE);
				viewChild.setClickable(false);
				viewChild.setEnabled(false);
			} else if (state.get(i).equals("4")) {
				winMoney = moneyFormat(getMoney.get(i));
				tvState.setText("￥" + moneyFormat(getMoney.get(i)));
				tvState.setTextColor(getResources().getColor(R.color.kjxx_red));
			} else {
				winMoney = "";
				tvState.setText(childStateTransfer(state.get(i)));
				if (state.get(i).equals("2")) {
					arrow.setVisibility(View.INVISIBLE);
					viewChild.setClickable(false);
					viewChild.setEnabled(false);
				}
			}
			if (mainState.equals("2") && state.get(i).equals("0")) {
				EDebug.o(this, "asdfasdfdddddddddddddddddssssss");
				tvState.setText("已取消");
				arrow.setVisibility(View.INVISIBLE);
				viewChild.setClickable(false);
				viewChild.setEnabled(false);
				btnChild.setVisibility(View.GONE);
			}
			// 点击进入购彩详情
			viewChild.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					EDebug.o(this, _i);
					startActivity(new Intent(TrackingRecordDetailMoreAct.this, LotteryRecordDetailAct.class).putExtra("lotid", lotID).putExtra("id", cpid.get(_i)).putExtra("winMoney", winMoney));
				}
			});

			linearChildList.addView(viewChild);
		}
	}

	// 控制金钱显示位数（待整理）
	public String moneyFormat(String str) {
		double userRestTemp = Double.parseDouble(str);
		DecimalFormat format = new DecimalFormat("0.00");
		String afterBet = format.format(userRestTemp);
		return afterBet;
	}

	// 判断是否全部取消追号
	public boolean isAllCancle() {
		boolean isAllCancle = true;
		for (int i = 0; i < linearChildList.getChildCount(); i++) {
			if (((Button) linearChildList.getChildAt(i).findViewById(R.id.childtracking_each_line_btn_cancle)).getVisibility() == View.VISIBLE) {
				isAllCancle = false;
				break;
			}
		}
		return isAllCancle;
	}

	public String childStateTransfer(String str) {
		String childState = null;
		switch (Integer.valueOf(str)) {
		case 0:
			childState = "等待中";
			break;
		case 1:
			childState = "未开奖";
			break;
		case 2:
			childState = "已取消";
			break;
		case 3:
			childState = "未中奖";
			break;
		case 4:
			childState = "中奖";
			break;
		default:
			break;
		}

		return childState;
	}

	// 撤销追号线程
	public void cancleTrackingThread(final String type, final String id) {
		new Thread() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				cancleCommunicater = new CancleTrackingCommunicater();
				Message msg = new Message();
				try {
					boolean isSucceed = cancleCommunicater.isCancleTracking(lotID, type, id);
					serverMsg = cancleCommunicater.getServerMsg();
					if (isSucceed) {
						msg = detailHandler.obtainMessage(CANCLE_TRACKING_SUCCEED, null);
						detailHandler.sendMessage(msg);
					} else {
						msg = detailHandler.obtainMessage(CANCLE_TRACKING_FAILED, null);
						detailHandler.sendMessage(msg);
					}
				} catch (Exception e) {
					// TODO: handle exception
					msg = detailHandler.obtainMessage(Config.CONNECTION_FAIL, null);
					detailHandler.sendMessage(msg);
				}
			}
		}.start();
	}

	public void showTipDialog(String str) {
		if (isShow) {
			ViewerUtil.showTipDialog(TrackingRecordDetailMoreAct.this, str, false);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.trackingrecord_detailmore_act_btn_cancleAllTracking:

			// 撤销所有追号
			isAllCancle = true;
			final Dialog tipDialog = new Dialog(TrackingRecordDetailMoreAct.this, R.style.dialog);
			tipDialog.getWindow().setContentView(R.layout.dialog_quit);
			((TextView) tipDialog.getWindow().findViewById(R.id.dialogquit_tv_title)).setText("取消追号任务");
			((TextView) tipDialog.getWindow().findViewById(R.id.dialogquit_tv_msg)).setText("该操作将取消所有等待中的追号任务，确定要终止该任务吗？");
			((Button) tipDialog.getWindow().findViewById(R.id.dialogquit_btn_cancel)).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tipDialog.cancel();
				}
			});
			((Button) tipDialog.getWindow().findViewById(R.id.dialogquit_btn_submit)).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tipDialog.cancel();
					progressDialog.show();
					cancleTrackingThread(MAIN_TASK, mainID);
				}
			});
			tipDialog.show();

			break;

		case R.id.trackingrecord_detailmore_act_btn_back:

			TrackingRecordDetailMoreAct.this.finish();

			break;
		}
	}
}
